package com.zking.web.ypf;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.catalina.util.RequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.zking.dao.domain.Information;
import com.zking.dao.domain.MyResume;
import com.zking.dao.domain.User;
import com.zking.service.ypf.INserteCom;
import com.zking.service.ypf.MyResumeService;
import com.zking.utils.RequestUtils;

@Controller
public class MyloginController {	
	@Autowired
	MyResumeService myResumeService;
	
	@Autowired
	private INserteCom inse;
	//	//首页
	//	@RequestMapping("/index")
	//	public String index() {
	//		return "index";
	//	}
	//
	//	//登录
	//	@RequestMapping(value = "/login")
	//	public String login(String username,String password) {
	//		Subject currentUser=SecurityUtils.getSubject();
	//		//判断当前用户是否验证
	//		if(!currentUser.isAuthenticated()) {
	//			//创建一个token
	//			UsernamePasswordToken token=new UsernamePasswordToken(username, password);
	//			//是不是记住我
	//			token.setRememberMe(true);
	//			try {
	//				// 执行登录. 
	//				currentUser.login(token);
	//			} catch (AuthenticationException ae) {
	//				System.out.println("登录失败: " + ae.getMessage());
	//			}
	//		}
	//		//已经验证了
	//		return "redirect:/index.jsp";
	//	}
	
	@RequestMapping(value = "/login",method =RequestMethod.POST )
	public String login(String userName,String passWord,HttpServletRequest request,MyResume myResume,Information information,HttpSession session) {
		System.out.println("login");
		System.out.println(userName+"  "+passWord);
		try {
			UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);
			Subject subject = SecurityUtils.getSubject();
			subject.login(token);
			User user=(User) subject.getPrincipal();
			System.out.println("我的当前的User===="+user);
			session.setAttribute("com_audit", user);
//			if("2".equals(user.getRoleid())) {
			myResume.setUser_id(user.getId());
			System.out.println(myResume.getUser_id());
			myResumeService.insert(myResume.getUser_id());
//			}else if("1".equals(user.getRoleid())) {
//				information.setInformation_id(user.getId());
//				inse.ins(information);
//			}
				myResume.setUser_id(user.getId());
				
//				System.out.println("myResume.getUser_id()"+myResume.getUser_id());
//				myResumeService.insert(myResume);
//				information.setInformation_id(user.getId());
//				inse.ins(information);
				
		} catch (DisabledAccountException e) {
			System.out.println("账户已被禁用");
			request.setAttribute("msg", "账户已被禁用");
			return "login";
		}catch(AuthenticationException e) {
			System.out.println("用户名或密码错误");
			request.setAttribute("msg", "用户名或密码错误");
			return "login";
		}
		Object object=session.getAttribute("com_audit");
		User user=(User) object;
		if ("1".equals(user.getRoleid())) {
			information.setInformation_id(user.getId());
			Information com_audit=inse.selectCom_audit(information);
			System.out.println("com_audit.getCom_audit()"+com_audit.getCom_audit());
			System.out.println("user.getRoleid())"+user.getRoleid());
			if("未审核".equals(com_audit.getCom_audit())&&"1".equals(user.getRoleid())) {
				request.setAttribute("msg", "你的公司还未审核成功");
				return "login";
			}else {
				// 执行到这里说明用户已登录成功
				System.out.println("成功");
				return "redirect:/index";
			}
			
		}else {
			return "redirect:/index";
			
		}
		//user.getId();
		
	}
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginPage() {
		System.out.println("loginget");
		return "login";
	}
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(HttpServletRequest request) {
		String username = "未登录";
		User currentLoginUser=RequestUtils.MycurrentLoginUser();
		System.out.println("currentLoginUser"+currentLoginUser);
		if (currentLoginUser != null && StringUtils.isNotEmpty(currentLoginUser.getUserName())) {
			username = currentLoginUser.getUserName();
			request.getSession().setAttribute("user", currentLoginUser);
			//currentLoginUser.getRoleid();
		} else {
			System.out.println("到了这里");
			return "redirect:/login";
		}
		//request.setAttribute("username", username);
		//request.getSession().setAttribute(name, value);
		if("1".equals(currentLoginUser.getRoleid())) {
			return "redirect:/queryly?information_id="+currentLoginUser.getId();
		}else if("2".equals(currentLoginUser.getRoleid())) {
			return "redirect:/seelp";
		}else if("3".equals(currentLoginUser.getRoleid())){
			return "redirect:/seelp";
		}else {
			return "redirect:/login";
		}
			
		
	}
}
